package com.vector.demo.controller.teacher;

import com.vector.demo.pojo.*;
import com.vector.demo.service.*;
import org.apache.ibatis.ognl.IntHashMap;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    ClassService classService;
    @Autowired
    GradeService gradeService;
    @Autowired
    AssignmentService assignmentService;
    @Autowired
    TaskService taskService;

    @RequestMapping("/queryAllTeacher")
    public void queryAllTeacher() {
        List<Teacher> teacherList = teacherService.queryAllTeacher();
        System.out.println(teacherList);
    }

    @ResponseBody
    @RequestMapping("doLogin")
    public String doLogin(String userName, String pwd) {
        System.out.println("登录教师名为" + userName);
        Teacher teacher = teacherService.queryTeacherByNameAndPassword(userName, pwd);
        if (teacher != null) {
            return "教师登录成功";
        } else
            return "登录失败,请检查账号密码";
    }

    @RequestMapping("/Menu")
    public String Menu(HttpSession session, String username, String pwd) {
        Teacher teacher = teacherService.queryTeacherByNameAndPassword(username, pwd);
        if (session.getAttribute("teacher") == null) {
            session.setAttribute("teacher", teacher);
        }
        return "teacher/teacher_index";
    }

    @RequestMapping("/queryAllGrade")
    public String queryAllGrade(Model model) {
        List<QueryGradeInfo> gradeList = gradeService.queryAllGrade();
        model.addAttribute("gradeList", gradeList);
        return "teacher/selectGrade";
    }

    @ResponseBody
    @RequestMapping("/check")
    public List<Integer> check(Model model) {
        List<QueryJiyiGrade> jiyiList = gradeService.queryJiyiGrade();
        int you = 0;
        int liang = 0;
        int zhong = 0;
        int cha = 0;
        int cai = 0;
        for (int i = 0; i < jiyiList.size(); i++) {
            if (jiyiList.get(i).getG_grade() >= 90) {
                you++;
            }
            if (jiyiList.get(i).getG_grade() >= 80 && jiyiList.get(i).getG_grade() < 90) {
                liang++;
            }
            if (jiyiList.get(i).getG_grade() >= 70 && jiyiList.get(i).getG_grade() < 80) {
                zhong++;
            }
            if (jiyiList.get(i).getG_grade() >= 60 && jiyiList.get(i).getG_grade() < 70) {
                cha++;
            } else
                cai++;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(cai);
        arrayList.add(cha);
        arrayList.add(zhong);
        arrayList.add(liang);
        arrayList.add(you);
        return arrayList;
    }

    @ResponseBody
    @RequestMapping("/average")
    public List average() {
        ArrayList<Integer> averageList = new ArrayList<>();
        int id1 = 1;
        int id2 = 2;
        int id3 = 3;
        int id4 = 4;
        int avg1 = gradeService.queryEachGrade(id1);
        int avg2 = gradeService.queryEachGrade(id2);
        int avg3 = gradeService.queryEachGrade(id3);
        int avg4 = gradeService.queryEachGrade(id4);
        averageList.add(avg1);
        averageList.add(avg2);
        averageList.add(avg3);
        averageList.add(avg4);
        System.out.println(averageList);
        return averageList;
    }

    @RequestMapping("/toUpdate")
    public String toUpdate() {
        return "teacher/update";
    }

    @ResponseBody
    @RequestMapping("/doUpdate")
    public String doUpdate(int tid, String oldpwd, String newpwd, HttpSession session) {
        Teacher teacher = teacherService.queryTeacherById(tid);
        if (teacher.getT_pwd().equals(oldpwd)) {
            teacher.setT_pwd(newpwd);
            int i = teacherService.updateTeacher(teacher);
            session.invalidate();
            return "1";
        } else
            return "原密码错误";
    }

    @RequestMapping("/returnLogin")
    public String returnLogin() {
        return "redirect:/login";
    }

    @RequestMapping("/selectAssignment2")
    public String selectAssignment2( Model model) {

        return "teacher/correct";
    }
    @ResponseBody
    @RequestMapping("/selectAssignment3")
    public List<Assignment> selectAssignment3(String teacher_name,String class_name,Model model){
        List<Assignment> assignmentList = assignmentService.selectAssignment2(teacher_name, class_name);
        return assignmentList;
    }

    @RequestMapping("/correcthomework")
    public String correcthomework(int ass_id, Model model) {
        Assignment assignment = assignmentService.selectAssignmentById(ass_id);
        Task task = taskService.queryTaskById(assignment.getAss_task_id());
        model.addAttribute("assignment", assignment);
        model.addAttribute("task", task);
        return "teacher/correcthomework";
    }

    @ResponseBody
    @RequestMapping("/corrcetsuccess")
    public String corrcetsuccess(String grade, int ass_id) {
        System.out.println(grade);
        System.out.println(ass_id);
        int status = 1;
        int i = assignmentService.updateGrade(grade, status,ass_id);
        if (i > 0)
            return "1";
        else
            return "0";
    }
}
